layui.use(['layer', 'form', 'table', 'admin', 'ax', 'func','laydate'], function () {
    var $ = layui.$;
    var layer = layui.layer;
    var form = layui.form;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;
    var func = layui.func;
    var laydate = layui.laydate;

    /**
     * 系统管理--消息管理
     */
    var Notice = {
        tableId: "noticeTable"    //表格id
    };

    //渲染时间选择框
    // laydate.render({
    //     elem: '#timeLimit',
    //     range: true,
    //     trigger: 'click',
    //     // max: Feng.currentDate()
    // });

    laydate.render({
        elem: '#riqi',
        range: true,
        trigger: 'click',
        // max: Feng.currentDate()
    });

    /**
     * 获取车号
     */
    var qyAjax = new $ax(Feng.ctxPath + "/checi/cheliangList");
    var qyResult = qyAjax.start();
    $.each(qyResult, function (index, val) {
        $("#chehao").append("<option value='" + val.chehao + "'>" + val.chehao + "</option>");
    });
    form.render('select');

    var qyAjax = new $ax(Feng.ctxPath + "/checi/dmList");
    var qyResult = qyAjax.start();
    //$("#peisongMendian").html("");
    $.each(qyResult, function (index, val) {
        $("#mendian").append("<option value='" + val.dm + "'>" + val.dm + "</option>");
    });
    form.render('select');

    /**
     * 检索省
     */
    var qyAjax = new $ax(Feng.ctxPath + "/area/getAreaByType?type=2");
    var qyResult = qyAjax.start();
    $.each(qyResult, function (index, val) {
        $("#qy").append("<option value='" + val.id + "'>" + val.simpleName + "</option>");
    });
    form.render('select');
    /**
     * 根据省检索市
     */
    form.on('select(qy)', function (data) {
        var qy = data.value;
        var areaAjax = new $ax(Feng.ctxPath + "/area/getAreaByPid");
        areaAjax.set("pid", qy);
        var areaResult = areaAjax.start();
        $("#peisongLuxian").html("");
        $("#peisongLuxian").append("<option value=''>请选择</option>");
        $.each(areaResult, function (index, val) {
            $("#peisongLuxian").append("<option value='" + val.simpleName + "'>" + val.simpleName + "</option>");
        });
        form.render('select');
    });
    /**
     * 根据市检索对应的门店
     */
    // form.on('select(peisongLuxian)', function (data) {
    //     var qyAjax = new $ax(Feng.ctxPath + "/checi/dmList");
    //     qyAjax.set("qy", data.value);
    //     var qyResult = qyAjax.start();
    //     //
    //     $("#peisongMendian").html("");
    //     $.each(qyResult, function (index, val) {
    //         $("#peisongMendian").append("<option value='" + val.dm + "'>" + val.dm + "</option>");
    //     });
    //     formSelects.render('peisongMendian');
    // });

    /**
     * 初始化表格的列
     */
    Notice.initColumn = function () {
        return [[
            {type: 'checkbox'},
            {field: 'id', align: "center", hide: true, sort: true, title: 'id'},
            {field: 'riqi', align: "center", title: '日期'},
            {field: 'facheTime', align: "center",title: '发车时间'},
            {field: 'chehao', align: "center", title: '车号'},
            {field: 'peisongLuxian', align: "center", title: '配送路线'},
            {field: 'peisongMendian', align: "center",hide: true, title: '配送门店', width: 300},
            {field: 'md', align: "center",  title: '配送门店', width: 300},
            {field: 'fzr', align: "center", title: '承运人姓名'},
            {field: 'phone', align: "center",  title: '承运人手机号'},
            {field: 'siji', align: "center", title: '司机'},
            {field: 'dianhua', align: "center",  title: '电话'},
            {field: 'beizhu', align: "center",  title: '备注'},
            {field: 'createrName', align: "center", title: '创建人'},
            {field: 'createTime', align: "center",  title: '创建时间'},
            {align: 'center', toolbar: '#tableBar', title: '操作', minWidth: 200}
        ]];
    };

    /**
     * 点击查询按钮
     */
    Notice.search = function () {
        var queryData = {};
       // queryData['timeLimit'] = $("#timeLimit").val();
        queryData['riqi'] = $("#riqi").val();
        queryData['sheng'] = $("#qy").val();
        queryData['shi'] = $("#peisongLuxian").val();
        queryData['chehao'] = $("#chehao").val();
        queryData['siji'] = $("#siji").val();
        queryData['mendian'] = $("#mendian").val();
        table.reload(Notice.tableId, {
            where: queryData, page: {curr: 1}
        });
    };

    /**
     * 点击清空按钮
     */
    $('#btnQingkong').click(function () {
        $("#riqi").val("");
        $("#qy").val("");
        $("#peisongLuxian").val("");
        $("#chehao").val("");
        $("#siji").val("");
        $("#mendian").val("");
        form.render('select');
        Notice.search();
    });

    /**
     * 弹出添加
     */
    Notice.openAddNotice = function () {
        func.open({
            height: 800,
            title: '添加配送车次',
            content: Feng.ctxPath + '/checi/checi_add',
            tableId: Notice.tableId
        });
    };

    /**
     * 点击编辑
     *
     * @param data 点击按钮时候的行数据
     */
    Notice.onEditNotice = function (data) {
        func.open({
            height: 800,
            title: '修改配送车次',
            content: Feng.ctxPath + "/checi/checi_update?id=" + data.id,
            tableId: Notice.tableId
        });
    };

    /**
     * 点击删除通知
     *
     * @param data 点击按钮时候的行数据
     */
    Notice.onDeleteNotice = function (data) {
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/checi/delete", function (data) {
                Feng.success("删除成功!");
                table.reload(Notice.tableId);
            }, function (data) {
                Feng.error("删除失败!" + data.responseJSON.message + "!");
            });
            ajax.set("id", data.id);
            ajax.start();
        };
        Feng.confirm("是否删除?", operation);
    };

    /**
     * 点击发送消息
     * @param data 点击按钮时候的行数据
     */
    Notice.confirmOk = function () {
        var checkRows = table.checkStatus(Notice.tableId);
        var data = checkRows.data;
        var ids = "";
        if (checkRows.data.length === 0) {
            Feng.error("请选择要发送的数据");
        } else {
            for (var i in data) {
                ids += data[i].id + ",";
            }
            var ajax = new $ax(Feng.ctxPath + "/checi/send", function (data) {
                Feng.success("发送成功!");
                table.reload(Notice.tableId);
            }, function (data) {
                Feng.error("发送失败!" + data.responseJSON.message + "!");
            });
            ajax.set("ids", ids);
            ajax.start();
        }
    };

    // 渲染表格
    var tableResult = table.render({
        elem: '#' + Notice.tableId,
        url: Feng.ctxPath + '/checi/list',
        page: true,
        height: "full-98",
        cellMinWidth: 100,
        cols: Notice.initColumn()
    });

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        Notice.search();
    });

    // 添加按钮点击事件
    $('#btnAdd').click(function () {
        Notice.openAddNotice();
    });

    // 发送按钮点击事件
    $('#send').click(function () {
        Notice.confirmOk();
    });

    // 工具条点击事件
    table.on('tool(' + Notice.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'edit') {
            Notice.onEditNotice(data);
        } else if (layEvent === 'delete') {
            Notice.onDeleteNotice(data);
        }
    });
});
